﻿
$(function () { 
    function reload_Captcha() {
        $(document).ready(function () {
            var xhttp;
            xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function () {
                if (xhttp.readyState == 4 && xhttp.status == 200) {
                    document.getElementById("captcha_area").innerHTML = xhttp.responseText;
                }
            };
            xhttp.open("GET", "../ajax/asp/loadcaptcha.aspx", true);
            xhttp.send();
        });
    }
    function sleep(milliseconds) {
        var start = new Date().getTime();
        for (var i = 0; i < 1e7; i++) {
            if ((new Date().getTime() - start) > milliseconds) {
                break;
            }
        }
    }
    function getCookie(name) {
        var cname = name + "=";
        var dc = document.cookie;
        if (dc.length > 0) {
            begin = dc.indexOf(cname);
            if (begin != -1) {
                begin += cname.length;
                end = dc.indexOf(";", begin);
                if (end == -1) end = dc.length;
                return unescape(dc.substring(begin, end));
            }
        }
        return null;
    }
    //$(document).ready(function () {
    //    var xhttp;
    //    xhttp = new XMLHttpRequest();
    //    xhttp.onreadystatechange = function () {
    //        if (xhttp.readyState == 4 && xhttp.status == 200) {
    //            document.getElementById("captcha_area").innerHTML = xhttp.responseText;
    //        }
    //    };
    //    xhttp.open("GET", "../Captcha.aspx", true);
    //    xhttp.send();
    //});
    $("#reload_captcha").click(function () {
        reload_Captcha();
    });
    $('.class-reg-drop').on('change', function () {
        var giatri = $(this).val();
        if (giatri == 0) {
            $(this).parent().children('.khac').animate({ height: 'toggle' }, "fast");
        }
        else $(this).parent().children('.khac').fadeOut();
    });
    $('#lop0').click(function () {
        $(this).parent().children('.khac').animate({ height: 'toggle' }, "fast");
    });

    $("#contact_request").on("keyup", function () {
        var str = $("#contact_request").val();
        var n = str.length;
        document.getElementById("request_lenght").innerHTML = n;
    });

    $("#contact-month").on("change", function () {
        var day = document.getElementById("contact-day").value;
        var month = document.getElementById("contact-month").value;
        var year = document.getElementById("contact-year").value;
        if (month == 2) {
            if (day > 29) {
                if (year % 4 == 0)
                    document.getElementById("contact-day").value = 29;
                else
                    document.getElementById("contact-day").value = 28;
            }
        }
        else {
            if (month == 4 || month == 6 || month == 9 || month == 11) {
                if (day == 31)
                    document.getElementById("contact-day").value = 30;
            }
        }
    });

    $("#contact-day").on("change", function () {
        var day = document.getElementById("contact-day").value;
        var month = document.getElementById("contact-month").value;
        var year = document.getElementById("contact-year").value;

        if (month == 2 && day > 28) {
            if (year % 4 == 0)
                document.getElementById("contact-day").value = 29;
            else
                document.getElementById("contact-day").value = 28;
        }
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            if (day == 31) {
                document.getElementById("contact-day").value = 30;
            }
        }
    });

    $("#contact-year").on("change", function () {
        var day = document.getElementById("contact-day").value;
        var month = document.getElementById("contact-month").value;
        var year = document.getElementById("contact-year").value;

        if (year % 4 == 0) {
            if (month == 2 && day > 29)
                document.getElementById("contact-day").value = 29;
        }
        else {
            if (month == 2 && day > 28)
                document.getElementById("contact-day").value = 28;
        }
    });

    $("#contact_email").click(function () {
        document.getElementById("contact_email").style.color = "black";
    });
    $("#btn_send_contact").click(function () {
        var fullname = document.getElementById("contact_fullname").value;
        var day = document.getElementById("contact-day").value;
        var month = document.getElementById("contact-month").value;
        var year = document.getElementById("contact-year").value;
        var birthday = year + "-" + month + "-" + day;
        var sex = 1;
        var captcha = document.getElementById("txt_captcha").value;
        var cls = "";
        if ($("#class-reg-female").is(":checked"))
            sex = 0;
        var email = document.getElementById("contact_email").value;
        var phone = document.getElementById("contact_phone").value;
        var address = document.getElementById("contact_address").value;
        var job = document.getElementById("contact_job").value;
        var sl_traditional = document.getElementById("contact_traditional").value;
        var traditional = "";
        var sl_purpose = document.getElementById("contact_purpose").value;
        var purpose = "";
        var sl_degree = document.getElementById("contact_degree").value;
        var degree = "";
        var captcha = document.getElementById("txt_captcha").value;
        var request = $("#contact_request").val();
        if (fullname.trim() == "") {
            document.getElementById("error-modal").style.display = "block";
            document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Họ và tên không được để trống";
            setTimeout("$('#error-modal').fadeOut()", 3000);
        }
        else {
            if (email.indexOf("@") == -1) {
                document.getElementById("error-modal").style.display = "block";
                document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Email không đúng định dạng (vd: senquocte@gmail.com)";
                setTimeout("$('#error-modal').fadeOut()", 3000);
            }
            else {
                var split = email.split("@");
                if (split.length > 2) {
                    document.getElementById("error-modal").style.display = "block";
                    document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Email không đúng định dạng (vd: senquocte@gmail.com)";
                    setTimeout("$('#error-modal').fadeOut()", 3000);
                }
                else {
                    if (email.indexOf(".com") == -1) {
                        document.getElementById("error-modal").style.display = "block";
                        document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Email không đúng định dạng (vd: senquocte@gmail.com)";
                        setTimeout("$('#error-modal').fadeOut()", 3000);
                    }
                    else {
                        if (phone.trim() == "") {
                            document.getElementById("error-modal").style.display = "block";
                            document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Hãy nhập số điện thoại để chúng tôi có thể liên hệ với các bạn";
                            setTimeout("$('#error-modal').fadeOut()", 3000);
                        } else {

                            if (sl_traditional != 0)
                                traditional = $("#contact_traditional option:selected").text();
                            else
                                traditional = "Khác: " + document.getElementById("contact_traditional_other").value;
                            if (sl_purpose != 0)
                                purpose = $("#contact_purpose option:selected").text();
                            else
                                purpose = document.getElementById("contact_purpose_other").value;
                            if (sl_degree != 0) {
                                degree = $("#contact_degree option:selected").text();
                            }
                            else
                                degree = document.getElementById("contact_degree_other").value;
                            if (degree == "") {
                                document.getElementById("error-modal").style.display = "block";
                                document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Hãy nhập Trình Độ bạn muốn đăng ký";
                                setTimeout("$('#error-modal').fadeOut()", 3000);
                            }
                            else {
                                for (var i = 1; i <= 4; i++) {
                                    if ($("#lop" + i).is(":checked")) {
                                        if (i > 1 && i <= 4 && cls!="")
                                            cls += "<br /> ";
                                        cls += $("#lop" + i).val();
                                    }
                                }
                                if ($("#lop0").is(":checked")) {
                                    var cls_other = document.getElementById("contact_class_other").value;
                                    if (cls != "") {
                                        cls += "<br />Khác: " + cls_other.trim();
                                    }
                                    else {
                                        cls += "Khác " + cls_other.trim();
                                    }
                                }
                                
                                if (cls == "") {
                                    document.getElementById("error-modal").style.display = "block";
                                    document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Hãy nhập Lớp bạn mong muốn đăng ký. VD: A1.8, B1.5";
                                    setTimeout("$('#error-modal').fadeOut()", 3000);
                                }
                                else {
                                    if (captcha != getCookie("Captcha")) {
                                        document.getElementById("error-modal").style.display = "block";
                                        document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Nhập mã Captcha không chính xác";
                                        setTimeout("$('#error-modal').fadeOut()", 3000);
                                        reload_Captcha();
                                    }
                                    else {
                                        //document.getElementById("success-modal").style.display = "block";
                                        //document.getElementById("success-modal").getElementsByClassName("success_content")[0].innerHTML = "Gửi thông tin thành công!<br/>Chúng tôi sẽ liên hệ với bạn ngay khi có lớp phù hợp.";
                                        //setTimeout("$('#success-modal').fadeOut()", 5000);
                                        $.post("../ajax/asp/send_Contact.aspx", { "fullname": fullname.trim(), "birthday": birthday, "sex": sex, "email": email, "phone": phone, "address": address, "job": job, "traditional": traditional, "purpose": purpose, "degree": degree, "cls": cls, "request": request });
                                        sleep(1000);
                                        window.location = "../trang-chu";
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    });
})